﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WarehouseWebAPP.code.BLL;

namespace WarehouseWebAPP.Pages.user.Search_Pages
{
    public partial class SearchDelivery : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        protected void ComponentComboBox_DataBound(object sender, EventArgs e)
        {
            ComponentComboBox.Items.Insert(0, new ListItem("--ALL--", "%"));
        }

        protected void UserComboBox_DataBound(object sender, EventArgs e)
        {
            UserComboBox.Items.Insert(0, new ListItem("--ALL--", "%"));
        }

        protected void CustomerComboBox_DataBound(object sender, EventArgs e)
        {
            CustomerComboBox.Items.Insert(0, new ListItem("--ALL--", "%"));
        }
        
        protected void DeliveryGridView_DataBound(object sender, EventArgs e)
        {
            foreach (GridViewRow row in DeliveryGridView.Rows)
            {
                Label user = (Label)row.FindControl("UserLabel");
                Label customer = (Label)row.FindControl("CustomerLabel");
                Label component = (Label)row.FindControl("ComponentLabel");
                if (user != null && customer != null && component != null)
                {
                    UserBLL userBLL = new UserBLL();
                    CustomerBLL customerBLL = new CustomerBLL();
                    ComponentBLL componentBLL = new ComponentBLL();


                    try
                    {
                        user.Text = userBLL.userGetByNum(new Guid(user.Text))[0].user_name;
                        customer.Text = customerBLL.CustomerGetByNum(new Guid(customer.Text))[0].customer_name;
                        component.Text = componentBLL.ComponentGetByNum(new Guid(component.Text))[0].component_serial_num;
                    }
                    finally
                    {
                        //TODO
                        //we should add dispose here
                        // we need to add dispose function foreach bll
                        // or maybe the detailsview disposes alone ,
                        // need to check, because there exists disposing event

                    }

                }
            }
        }

        protected void ExcelButton_Click(object sender, EventArgs e)
        {
            Response.ClearHeaders();
            Response.AddHeader("content-disposition", "attachment;filename=DeliveryReport.xls");
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            DeliveryGridView.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            return;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        

    }
}
